<template>
  <div class="hm-login">
    <div class="title">面经登录</div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          square
          block
          type="info"
          native-type="submit"
          color="#fa6d1d"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link to="/register" class="link">注册账号</router-link>
  </div>
</template>

<script>
import { login } from "@/api/user";
import { setToken } from "@/utils/storage";

export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      if (this.username === "" || this.password === "") {
        return;
      }
      const res = await login({
        username: this.username,
        password: this.password,
      });
      this.$toast("登录成功");
      setToken(res.data.data.token);
      this.$router.push("/layout/article/recommend");
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  text-align: center;
  height: 50px;
  line-height: 50px;
}
.link {
  padding-right: 5.33333vw;
  float: right;
  color: #006699;
  font-size: 12px;
}
</style>
